package com.bringway.p2p.dao.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Repository;

import com.bringway.framework.dao.P2PJdbcDao;
import com.bringway.p2p.dao.ComNewsDao;
import com.bringway.p2p.dao.SysDao;

import net.sf.json.JSONObject;

@Repository
public class ComNewsDaoImpl extends P2PJdbcDao implements ComNewsDao {

	private Logger logger = Logger.getLogger(ComNewsDaoImpl.class);

	@Override
	public List<Map<String, Object>> queryComNewsList(JSONObject params) {
		int start = params.optInt("start");
		int limit = params.optInt("limit");
		String startDate = params.optString("startDate");
		String endDate = params.optString("endDate");
		String title = params.optString("title");
		Map<String, Object> paramMap = new HashMap<>();
		paramMap.put("start", start);
		paramMap.put("limit", limit);
		StringBuffer sql = new StringBuffer();
		sql.append("select ID,TITLE,AUTHOR,ABSTRACT,TEXT_HTML,date_format(SHOW_DATE,'%Y-%m-%d') SHOW_DATE, OPER_USERID,date_format(CREATE_DATE,'%Y-%m-%d %H:%i:%s') CREATE_DATE,ISPUBLIC  from t_zhfw_com_news where 1=1");
		if (StringUtils.isNotEmpty(startDate)) {
			sql.append(" and date_format(CREATE_DATE,'%Y-%m-%d')>=:startDate");
			paramMap.put("startDate", startDate);
		}
		if (StringUtils.isNotEmpty(endDate)) {
			sql.append(" and date_format(CREATE_DATE,'%Y-%m-%d')<=:endDate");
			paramMap.put("endDate", endDate);
		}
		if (StringUtils.isNotEmpty(title)) {
			sql.append(" and TITLE like :title");
			paramMap.put("title", "%" + title + "%");
		}
		sql.append(" order by CREATE_DATE desc limit :start,:limit");
		logger.info("querySysinfo sql is " + sql.toString());
		logger.info("querySysinfo paramMap is " + paramMap);
		return this.getNamedParameterJdbcTemplate().queryForList(sql.toString(), paramMap);
	}

	@Override
	public int getComNewsCount(JSONObject params) {
		String startDate = params.optString("startDate");
		String endDate = params.optString("endDate");
		String title = params.optString("title");
		Map<String, Object> paramMap = new HashMap<>();
		StringBuffer sql = new StringBuffer();
		sql.append("select count(*) from t_zhfw_com_news  where 1=1");
		if (StringUtils.isNotEmpty(startDate)) {
			sql.append(" and date_format(CREATE_DATE,'%Y-%m-%d')>=:startDate");
			paramMap.put("startDate", startDate);
		}
		if (StringUtils.isNotEmpty(endDate)) {
			sql.append(" and date_format(CREATE_DATE,'%Y-%m-%d')<=:endDate");
			paramMap.put("endDate", endDate);
		}
		if (StringUtils.isNotEmpty(title)) {
			sql.append(" and TITLE like :title");
			paramMap.put("title", "%" + title + "%");
		}
		logger.info("getCount sql is " + sql.toString());
		logger.info("getCount paramMap is " + paramMap);
		return this.getNamedParameterJdbcTemplate().queryForInt(sql.toString(), paramMap);
	}

	@Override
	public int insertComNews(Map<String, Object> paramMap) {
		String sql = "insert into t_zhfw_com_news(TITLE,AUTHOR,ABSTRACT,TEXT_HTML,SHOW_DATE,OPER_USERID,CREATE_DATE) values(:title,:author,:abs,:texthtml,:showsj,:userid,now())";
		logger.info("insertSysinfo sql is " + sql);
		logger.info("insertSysinfo paramMap is " + paramMap);
		return this.getNamedParameterJdbcTemplate().update(sql, paramMap);
	}

	@Override
	public int updateComNews(Map<String, Object> paramMap) {
		String sql = "update t_zhfw_com_news set TITLE=:title,AUTHOR=:author,ABSTRACT=:abs,TEXT_HTML=:texthtml,SHOW_DATE=:showsj,ISPUBLIC='0' where ID=:id";
		logger.info("updateSysinfo sql is " + sql);
		logger.info("updateSysinfo paramMap is " + paramMap);
		return this.getNamedParameterJdbcTemplate().update(sql, paramMap);
	}

	@Override
	public int publicComNews(Map<String, Object> paramMap) {
		String sql = "update t_zhfw_com_news set ISPUBLIC=:ispub where ID=:id";
		logger.info("updateSysinfo sql is " + sql);
		logger.info("updateSysinfo paramMap is " + paramMap);
		return this.getNamedParameterJdbcTemplate().update(sql, paramMap);
	}

}
